<?php
/**
 * 分享跟踪操作类
 * @author lifw
 */
Class IOSS_Track {
	private static $_instance;
	private static $table = "affiliate_track";
	private static $requestLimitIntval = 60; // 1分钟/每IP
 	public static function getInstance(){
		if(self::$_instance == null){
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 * 通过account获取用户信息
	 * @param int $id
	 * @return IOSS_User
	 */
	public static  function getByPhone($affiliate_phone){
		$db = IOSS_DB::getInstance();
		$row = $db->where('affiliate_phone', $affiliate_phone)->get(self::$table)->row_array();
		if($row){
			return $row;
		}else{
			return null;
		}
	}

	// 创建跟踪记录
	function create($user_id, $phone, $ip){
		$db = IOSS_DB::getInstance();
		$time = time();
		$vo = new stdClass();
		$vo->user_id = $user_id;
		$vo->affiliate_phone = $phone;
		$vo->affiliate_ip = $ip; 
		$vo->create_time = time();
		if($db->insert(self::$table, $vo)){
			return $db->insert_id();
		}else{
			return false;
		}
	}

	function requestLimit($ip){
		$db = IOSS_DB::getInstance();
		$item = $db->where('affiliate_ip', $ip)->order_by('create_time', 'DESC')->limit(1)->get(self::$table)->row_array();
		log_message('ERROR', print_r($item, true));
		if ($item && time() - $item['create_time'] < self::$requestLimitIntval) {
			return false;
		}
		return true;
	}
}
